package com.design.mode.single;

import java.util.concurrent.TimeUnit;

/**
 * 懒汉式
 *
 * @Date 2023/3/29 15:08:19
 * @Author luopeng
 */
public class SingleObjectLazy {

    private static SingleObjectLazy object;
    private static SingleObjectLazy objectSecurity;

    private SingleObjectLazy() {
    }

    /**
     * 线程不安全的方式
     *
     * @return
     */
    public static SingleObjectLazy getSingleObject() {
        if (object == null) {
            try {
                TimeUnit.MILLISECONDS.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            object = new SingleObjectLazy();
        }
        return object;
    }

    /**
     * 线程安全的方式
     *
     * @return
     */
    public static synchronized SingleObjectLazy getSingleObjectSecurity() {
        if (objectSecurity == null) {
            try {
                TimeUnit.MILLISECONDS.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            objectSecurity = new SingleObjectLazy();
        }
        return objectSecurity;
    }

    public void showMessage() {
        System.out.println("Hello, World!");
    }
}
